export const getUrlKey = (name) => {
  return (
    decodeURIComponent(
      (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
        location.href
      ) || [''])[1]?.replace(/\+/g, '%20')
    ) || null
  );
};
//对象转数组
export const aimToArr = (Data) => {
  let arr = [];
  Object.keys(Data).map((key) => {
    let obj = {
      label: Data[key],
      value: key,
    };
    arr.push(obj);
  });
  return arr;
};
//to JSON string
/**
 *
 * @param {*} list
 */
export const toStringify = (list) => {
  if (list) {
    list.forEach((item) => {
      item.componentsList
        ? item.componentsList.forEach((componentsListItem) => {
            componentsListItem.closeValve = componentsListItem.closeValve
              ? JSON.stringify(componentsListItem.closeValve)
              : '';
            componentsListItem.openValve = componentsListItem.openValve
              ? JSON.stringify(componentsListItem.openValve)
              : '';
          })
        : '';
      item.groupLists ? toStringify(item.groupLists) : '';
    });
  }
};
export const toParse = (list) => {
  if (list) {
    list.forEach((item) => {
      item.componentsList
        ? item.componentsList.forEach((componentsListItem) => {
            componentsListItem.closeValve = componentsListItem.closeValve
              ? JSON.parse(componentsListItem.closeValve)
              : '';
              componentsListItem.openValve = componentsListItem.openValve
              ? JSON.parse(componentsListItem.openValve)
              : '';
          })
        : '';
      item.groupLists ? toParse(item.groupLists) : '';
    });
  }
};
export const imageUrlToBase64 = (networkUrl) => {
  return new Promise((resolve) => {
    //一定要设置为let，不然图片不显示
    let image = new Image();
    //解决跨域问题
    image.setAttribute('crossOrigin', 'anonymous');
    let imageUrl = networkUrl;
    image.src = imageUrl;
    //image.onload为异步加载
    image.onload = () => {
      var canvas = document.createElement('canvas');
      canvas.width = image.width;
      canvas.height = image.height;
      var context = canvas.getContext('2d');
      context.drawImage(image, 0, 0, image.width, image.height);
      var quality = 0.8;
      resolve(canvas.toDataURL('image/jpeg', quality)); //使用toDataUrl将图片转换成jpeg的格式,不要把图片压缩成png，因为压缩成png后base64的字符串可能比不转换前的长！
    };
  });
};
